const express = require("express");
const Router = express.Router();
const bodyParser = require("body-parser");
const { goodsList } = require("../../db/sql/db.js");
const { json } = require("../../db/json");
const db = require("../../db/sql/dbconfig");
const { RandNum } = require("../../utils/format");
const { updateTime } = require("../../utils/time");

Router.use(bodyParser.json());
Router.use(
  bodyParser.urlencoded({
    extended: false,
  })
);
Router.use(bodyParser.json());

// 获取分类
Router.get("/category", (req, res) => {
  db(goodsList.queryCategory, [], (result) => {
    res.send({
      code: 0,
      msg: "获取成功",
      result,
    });
  });
});

// 新增分类
Router.post("/addGoodCategory", (req, res) => {
  let { category_name, brand_name } = req.body;
  db(goodsList.addBrand, [category_name], (result) => {
    if (brand_name) {
      // 有商品品牌
      db(goodsList.queryCategory, [], (result) => {
        let item = result.find((item) => item.name == category_name);
        db(goodsList.addCate, [item.id, brand_name], (data) => {
          res.send({
            code: 0,
            msg: "新增成功",
          });
        });
      });
    } else {
      res.send({
        code: 0,
        msg: "新增成功",
      });
    }
  });
});

// 获取品牌(根据id)
Router.get("/brand", (req, res) => {
  let { id } = req.query;
  if (!id) {
    let result = "drop";
    json(res, result);
    return;
  }
  db(goodsList.queryBrand, [id], (result) => {
    res.send({
      code: 0,
      msg: "获取成功",
      result,
    });
  });
});

// 新增商品
Router.post("/add_goods", (req, res) => {
  let {
    title, // 商品标题
    price, // 商品价格
    putaway_status, // 是否上架
    found_id, // 创建人的id
    goods_image, // 商品主图
    banner, // 商品主图
    brand, // 品牌id
    category, // 分类id
    inventory, // 库存量
    desc, // 商品详情
  } = req.body;
  if (putaway_status == 0) {
    sure_time = "";
  } else {
    sure_time = updateTime();
  }

  console.log(brand, category);
  db(
    goodsList.addGoods,
    [
      RandNum(),
      title,
      price,
      putaway_status,
      found_id,
      goods_image,
      brand,
      category,
      updateTime(),
      sure_time,
      inventory,
      desc,
    ],
    (result) => {
      res.send({
        code: 0,
        msg: "新增成功",
      });
    }
  );
});

// 获取商品列表
Router.get("/getGoods", (req, res) => {
  let {
    //   good_name, // 商品名称
    //   isSure_time, // 商品发布时间
    //   goods_category, // 商品分类
    //   goods_bind, // 商品品牌
    //   putaway_status, // 上架状态
    //   check_status, // 审核状态
    page_size, //  当前页的最大容量
    currentPage, // 当前页
  } = req.query;
  const size = page_size ? page_size : 10;
  currentPage = currentPage ? currentPage - 1 : 0;
  db(
    goodsList.getList,
    [
      //   good_name,
      //   isSure_time,
      //   goods_category,
      //   goods_bind,
      //   putaway_status,
      //   check_status,
      currentPage,
      size,
    ],
    (result) => {
      res.send({
        code: 0,
        msg: "获取成功",
        data: {
          data: result,
          total: result.length,
          page_size: size,
          currentPage: currentPage,
        },
      });
    }
  );
});
// 修改商品
Router.post("/editGoods", (req, res) => {
  let {} = req.body;
  db(goodsList.edit_goods, [], (result) => {
    console.log(result);
  });
});
// 删除商品
Router.post("/deleteGoods", (req, res) => {
  let { id } = req.body;
  if (!id) {
    let result = "drop";
    json(res, result);
    return;
  }
  db(gooodList.del_goods, [id], (result) => {
    res.send({
      code: 0,
      msg: "成功删除商品",
    });
  });
});

// 获取所有的商品分类和商品品牌
Router.get("/getCategoryBrand", (req, res) => {
  let data = [
    // { parent: {child: []}, }
  ];
  // 获取商品分类
  db(goodsList.queryCategory, [], (result) => {
    result.forEach((item, index) => {
      data.push({ parent: item });
    });
  });
  // 获取商品品牌
  db(goodsList.queryAllBrand, [], (result) => {
    result.forEach((item, index) => {
      if (data[index].parent.id == item.category) {
        data[index].parent.child = [...result];
      }
    });
    res.send({
      code: 0,
      msg: "success",
      data,
      total: data.length,
    });
  });
});

module.exports = Router;
